﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using HotelManagement.Class;


namespace HotelManagement
{
    public partial class UpdateStaffMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                //Display Staff name to ListBox1
                displayStaffName();

                //Display Stadd ID to ListBox2
                displayStaffID();
            }
        }

        protected void btnRetrieveStaffByName_Click(object sender, EventArgs e)
        {
            try
            {
                Server.Transfer("UpdateStaff.aspx?StaffID=" + ListBoxRetrieveByName.SelectedValue);
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Select Staff Name to Search";
            }
        }

        protected void btnUpdateStaffbyID_Click(object sender, EventArgs e)
        {
            try
            {
                Server.Transfer("UpdateStaff.aspx?StaffID=" + ListBoxRetrieveByID.SelectedValue);
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Select Staff ID to Search";
            }
        }

        private void displayStaffName()
        {
            try
            {
            SqlConnection conn = StaffDBManager.ConnectToDatabase();
            ListBoxRetrieveByName.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName, StaffID FROM Staff";
                  
            
            SqlCommand cmd = new SqlCommand(selectSQL, conn);
            ListItem newItem = new ListItem();
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString();
                    ListBoxRetrieveByName.Items.Add(newItem);
                }
                reader.Close();


            }
            catch (SqlException Ex1)
            {
                lblStatus.Text = "Error: " + Ex1 + "Please try again";
            }
        }

        private void displayStaffID()
        {
            
            try
            {
                SqlConnection conn = StaffDBManager.ConnectToDatabase();

                DataTable objectData = new DataTable();
                string Query = "SELECT [StaffID] from [Staff] ORDER BY StaffID ASC";
                SqlDataAdapter objda = new SqlDataAdapter(Query, conn);
                conn.Open();
                objda.Fill(objectData);
                if (objectData.Rows.Count > 0)
                {
                    ListBoxRetrieveByID.DataSource = objectData;
                    ListBoxRetrieveByID.DataValueField = "StaffID";
                    ListBoxRetrieveByID.DataBind();
                }

                conn.Close();
            }
            catch (SqlException Ex1)
            {
                lblStatus.Text = "Error: " + Ex1 + "Please try again";
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }



    }
 }
    

